package com.ssy.lingxi.product.controller.pc.customer;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.request.CustomerCategoryAttributeRequest;
import com.ssy.lingxi.product.api.model.request.CustomerCategoryAttributeSortRequest;
import com.ssy.lingxi.product.api.model.response.CustomerAttributeResponse;
import com.ssy.lingxi.product.entity.customer.CustomerAttribute;
import com.ssy.lingxi.product.service.customer.ICustomerCategoryAttributeService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @menu: 商品能力--品类关联属性
 * 商品能力--品类关联属性管理类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/23
 */
@RestController
@RequestMapping("/product/customer")
public class CustomerCategoryAttributeController extends BaseController {

    @Resource
    private ICustomerCategoryAttributeService customerCategoryAttributeService;

    /**
     * 查询品类下的属性列表
     * @param pageVO 分页实体
     * @param name 属性名称
     * @param categoryId 品类id
     * @param isByCategory 是否查询该品类下的属性
     */
    @RequestMapping(value = "/getCustomerCategoryAttributeList", method = RequestMethod.GET)
    public Wrapper<PageData<CustomerAttributeResponse>> getCustomerCategoryAttributeList(PageVO pageVO, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "groupName", required = false) String groupName, @RequestParam(value = "categoryId") Long categoryId, @RequestParam(value = "isByCategory") Boolean isByCategory) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<CustomerAttribute> result = customerCategoryAttributeService.getCustomerCategoryAttributeList(sysUser, pageVO, name, groupName, categoryId, isByCategory);
        List<CustomerAttributeResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<CustomerAttributeResponse>>(){}.getType());
        resultList = resultList
                .stream()
                .filter(Objects::nonNull)
                .sorted(Comparator.comparing(CustomerAttributeResponse::getSort))
                .collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询品类下的属性列表-MRO模式 按照特定的排序返回结果
     * @param categoryId 品类id
     * @param isByCategory 是否查询该品类下的属性
     */
    @RequestMapping(value = "/getMroCustomerCategoryAttributeList", method = RequestMethod.GET)
    public Wrapper<List<CustomerAttribute>> getMroCustomerCategoryAttributeList(@RequestParam(value = "name", required = false) String name, @RequestParam(value = "groupName", required = false) String groupName, @RequestParam(value = "categoryId") Long categoryId, @RequestParam(value = "isByCategory") Boolean isByCategory) {
        UserLoginCacheDTO sysUser = new UserLoginCacheDTO();
        if (isLogin()) {
            sysUser  = this.getSysUser();
        }
        return Wrapper.success(customerCategoryAttributeService.getMroCustomerCategoryAttributeList(sysUser, name, groupName, categoryId, isByCategory));
    }

    /**
     * 新增/修改品类下的属性
     * @param customerCategoryAttributeRequest
     * @return
     */
    @RequestMapping(value = "/saveCustomerCategoryAttribute", method = RequestMethod.POST)
    public Wrapper<String> saveCustomerCategoryAttribute(@RequestBody CustomerCategoryAttributeRequest customerCategoryAttributeRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerCategoryAttributeService.saveCustomerCategoryAttribute(sysUser, customerCategoryAttributeRequest));
    }

    /**
     * 解除品类属性关系
     * @param customerCategoryAttributeRequest
     * @return
     */
    @RequestMapping(value = "deleteCustomerCategoryAttribute", method = RequestMethod.POST)
    public Wrapper<String> deleteCustomerCategoryAttribute(@RequestBody CustomerCategoryAttributeRequest customerCategoryAttributeRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerCategoryAttributeService.deleteCustomerCategoryAttribute(sysUser, customerCategoryAttributeRequest));
    }

    /**
     * 新增/修改品类下的属性排序
     *
     * @param customerCategoryAttributeSortRequestList
     * @return
     */
    @PostMapping("/saveCustomerCategoryAttributeSort")
    public Wrapper<Boolean> saveCustomerCategoryAttributeSort(@RequestBody List<CustomerCategoryAttributeSortRequest> customerCategoryAttributeSortRequestList) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerCategoryAttributeService.saveCustomerCategoryAttributeSort(sysUser, customerCategoryAttributeSortRequestList));
    }

}
